package actions;

import gui.ChatTab;
import gui.MainFrame;
import gui.MyTabbedPane;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JList;

import model.User;

@SuppressWarnings("serial")
public class PrivateChatAction extends AbstractAction {

	public PrivateChatAction() {
		super();
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public void actionPerformed(ActionEvent e) {
		JList list;
		if(e.getSource() instanceof JList) {
			list = (JList) e.getSource();
			User user =null;
			if(list.getSelectedValue() instanceof User) {
				user = (User) list.getSelectedValue();
				if(user.equals(MainFrame.getInstance().getCurrentUser())) return;
				MyTabbedPane jtp= MainFrame.getInstance().getTabbedPane();
				String title = "Chat with: " + user.toString();
				
				if(jtp.indexOfTab(title)==-1) {
					int index=jtp.myAddTab(title, new ChatTab(user));
					jtp.setSelectedIndex(index);
				}
				else {
					int index = jtp.indexOfTab(title);
					jtp.setSelectedIndex(index);
				}
			}
		}
		
	}

}
